package com.sunday.pinxinghui.model.account;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.MD5Utils;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.TimeCount;
import com.sunday.common.utils.ToastUtils;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.base.BaseApplication;
import com.sunday.pinxinghui.base.Constant;
import com.sunday.pinxinghui.entity.Member;
import com.sunday.pinxinghui.network.ApiClient;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by 刘涛 on 2017/8/11.
 */

public class PaySettingActivity extends BaseActivity {


    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.mobile)
    TextView mobile;
    @Bind(R.id.edit_code)
    EditText editCode;
    @Bind(R.id.btn_send_code)
    TextView btnSendCode;
    @Bind(R.id.edit_pwd)
    EditText editPwd;
    @Bind(R.id.edit_confirm_pwd)
    EditText editConfirmPwd;
    @Bind(R.id.btn_submit)
    TextView btnSubmit;
    private Member member;

    private TimeCount timeCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_setting);
        ButterKnife.bind(this);
        titleView.setText("支付密码设置");
        member = BaseApplication.getInstance().getMember();
        mobile.setText(member.getMobile());
        timeCount = new TimeCount(60000, 1000, btnSendCode);
    }

    @OnClick({R.id.btn_send_code, R.id.btn_submit})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_send_code:
                String mobile = member.getMobile();
                if (TextUtils.isEmpty(mobile) || !StringUtils.isMobileNO(mobile)) {
                    ToastUtils.showToast(mContext, "请输入正确的手机号");
                    return;
                }
                sendCode(mobile);
                break;
            case R.id.btn_submit:
                String code = editCode.getText().toString().trim();
                if (TextUtils.isEmpty(code)) {
                    ToastUtils.showToast(mContext, "请输入验证码");
                    return;
                }
                String pwd = editPwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd)) {
                    ToastUtils.showToast(mContext, "请输入密码");
                    return;
                }
                if (pwd.length() != 6) {
                    ToastUtils.showToast(mContext, "请输入6位支付密码");
                    return;
                }
                String pwd1 = editConfirmPwd.getText().toString().trim();
                if (!pwd1.equals(pwd)) {
                    ToastUtils.showToast(mContext, "两次密码不一致");
                    return;
                }
                showLoadingDialog(0);
                Call<ResultDO<Member>> call = ApiClient.getApiAdapter().updatePayPassword(member.getId(), code, MD5Utils.MD5(pwd));
                call.enqueue(new Callback<ResultDO<Member>>() {
                    @Override
                    public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                        if (isFinish || response.body() == null) {
                            return;
                        }
                        dissMissDialog();
                        ResultDO<Member> resultDO = response.body();
                        if (response.body().getCode() == 0) {
                            ToastUtils.showToast(mContext, "修改成功！");
                            finish();
                        } else {
                            ToastUtils.showToast(mContext, resultDO.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(Call<ResultDO<Member>> call, Throwable t) {
                        dissMissDialog();
                        ToastUtils.showToast(mContext, R.string.network_error);
                    }
                });
                break;
        }
    }

    private void sendCode(String mobile) {
        Call<ResultDO> call = ApiClient.getApiAdapter().sendCode(null, mobile, Constant.CODE_FOUR);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext, "验证码已发送到手机");
                    timeCount.start();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timeCount != null) {
            timeCount.cancel();
        }
    }


}
